package cn.stylefeng.guns.modular.emergency.controller;

import cn.stylefeng.guns.core.consts.ProjectConstants;
import cn.stylefeng.guns.modular.emergency.entity.Emergency;
import cn.stylefeng.guns.modular.emergency.pojo.request.EmergencyRequest;
import cn.stylefeng.guns.modular.emergency.pojo.response.EmergencyResponse;
import cn.stylefeng.guns.modular.emergency.service.EmergencyService;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应急预案表	控制器
 *
 * @author xbz
 * @date 2023/04/24 14:58
 */
@RestController
@ApiResource(name = "应急预案表	")
public class EmergencyController {

    @Resource
    private EmergencyService emergencyService;

    /**
     * 添加
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    @PostResource(name = "添加", path = "/emergency/add",requiredPermission = false,requiredLogin = false)
    public ResponseData add(@RequestBody @Validated(EmergencyRequest.add.class) EmergencyRequest emergencyRequest) {

        return emergencyService.add(emergencyRequest);
    }

    /**
     * 删除
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    @PostResource(name = "删除", path = "/emergency/delete",requiredPermission = false,requiredLogin = false)
    public ResponseData<?> delete(@RequestBody @Validated(EmergencyRequest.delete.class) EmergencyRequest emergencyRequest) {
        emergencyService.del(emergencyRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    @PostResource(name = "编辑", path = "/emergency/edit",requiredPermission = false,requiredLogin = false)
    public ResponseData edit(@RequestBody @Validated(EmergencyRequest.edit.class) EmergencyRequest emergencyRequest) {

        return emergencyService.edit(emergencyRequest);
    }

    /**
     * 查看详情
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    @GetResource(name = "查看详情", path = "/emergency/detail",requiredPermission = false,requiredLogin = false)
    public ResponseData<Emergency> detail(@Validated(EmergencyRequest.detail.class) EmergencyRequest emergencyRequest) {
        return new SuccessResponseData<>(emergencyService.detail(emergencyRequest));
    }

    /**
     * 获取列表
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    @GetResource(name = "获取列表", path = "/emergency/list",requiredPermission = false,requiredLogin = false)
    public ResponseData<List<Emergency>> list(EmergencyRequest emergencyRequest) {
        return new SuccessResponseData<>(emergencyService.findList(emergencyRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    @GetResource(name = "分页查询", path = "/emergency/page",requiredPermission = false,requiredLogin = false)
    public ResponseData<PageResult<Emergency>> page(EmergencyRequest emergencyRequest) {
        return new SuccessResponseData<>(emergencyService.findPage(emergencyRequest));
    }

    @GetResource(name = "分页查询该节点及以下的应急预案", path = "/emergency/getBelowEmergency",requiredPermission = false,requiredLogin = false)
    public ResponseData<PageResult<EmergencyResponse>> getBelowEmergency(EmergencyRequest emergencyRequest) {
        String tableName = ProjectConstants.EPC_EMERGENCY_TYPE_TABLE_NAME;
        return new SuccessResponseData<>(emergencyService.getBelowEmergency(tableName,emergencyRequest.getId(),emergencyRequest.getEmergencyName(),emergencyRequest.getPageSize(),emergencyRequest.getPageNo()));
    }

}
